import ErrorComp from '@/components/common/error-comp';
import LeftToRightView from '@/components/common/motion/left-to-right-view';
import ProductTable from '@/components/shop/product-table';
import { listProductsByPage } from '@/service/query/product';
import {
  jsonToDateRangeSchema,
  pageSchema,
} from '@/service/query/schema/common';
import { shopProductSearchSchema } from '@/service/query/schema/product';

export const metadata = {
  title: '外卖管理',
};

const PageSchema = pageSchema
  .merge(shopProductSearchSchema)
  .omit({
    createdDateRange: true,
    updatedDateRange: true,
  })
  .extend({
    createdDateRange: jsonToDateRangeSchema,
    updatedDateRange: jsonToDateRangeSchema,
  });

export default async function Page({
  searchParams,
}: {
  searchParams: Promise<{ [key: string]: string | string[] | undefined }>;
}) {
  const params = PageSchema.safeParse(await searchParams);
  if (!params.success) return <ErrorComp message="参数错误" />;

  const res = listProductsByPage({
    page: params.data.page,
    pageSize: params.data.pageSize,
    id: params.data.id ? +params.data.id : undefined,
    name: params.data.name,
    description: params.data.description,
    price: params.data.price,
    createdDateRange: params.data.createdDateRange,
    updatedDateRange: params.data.updatedDateRange,
  });

  return (
    <LeftToRightView>
      <div className="p-5 rounded-lg bg-background space-y-8">
        <div className="text-2xl font-bold">外卖管理</div>
        <ProductTable pageData={res} />
      </div>
    </LeftToRightView>
  );
}
